package com.fxkj.cloud.drms.infra.repository;

import com.fxkj.cloud.dbs.common.constants.DeviceStatusEnum;
import com.fxkj.cloud.dbs.common.model.DeviceAttributeKey;
import com.fxkj.cloud.dbs.common.model.DeviceBasicInfo;
import com.fxkj.cloud.drms.domain.device.repository.DeviceRepository;
import com.fxkj.cloud.drms.infra.feignApi.DeviceFeignApi;
import com.fxkj.cloud.drms.infra.persistence.cache.DeviceCacheRepository;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * <p>description: </p>
 * @author ww
 * @date 2025/3/19 16:07
 */
@Component
public class DeviceRepositoryImpl implements DeviceRepository {

    private final DeviceCacheRepository deviceCacheRepository;
    private final DeviceFeignApi deviceFeignApi;

    public DeviceRepositoryImpl(DeviceCacheRepository deviceCacheRepository, DeviceFeignApi deviceFeignApi) {
        this.deviceCacheRepository = deviceCacheRepository;
        this.deviceFeignApi = deviceFeignApi;
    }

    @Override
    public DeviceBasicInfo getDeviceBasicInfoByDeviceSerialNo(String code) {
        //从缓存获取
        DeviceBasicInfo deviceBasicInfoByDeviceSerialNo = deviceCacheRepository.getDeviceBasicInfoByDeviceSerialNo(code);
        if (null != deviceBasicInfoByDeviceSerialNo) {
            return deviceBasicInfoByDeviceSerialNo;
        }
        //从远程调用查询数据库获取 todo
        //可能有坑? 比如一直某个不存在库中的恶意的设备号一直请求，会一直查询数据库，导致数据库压力过大
        //可能考虑只走缓存
        return null;
    }

    @Override
    public DeviceBasicInfo getDeviceBasicInfoByDeviceId(UUID deviceId) {
        //从缓存获取
        DeviceBasicInfo deviceBasicInfoByDeviceId = deviceCacheRepository.getDeviceBasicInfoByDeviceId(deviceId);
        if (null != deviceBasicInfoByDeviceId) {
            return deviceBasicInfoByDeviceId;
        }
        //从远程调用查询数据库获取 todo
        //可能有坑? 比如一直某个不存在库中的恶意的设备号一直请求，会一直查询数据库，导致数据库压力过大
        //可能考虑只走缓存
        return null;
    }

    @Override
    public Map<UUID, DeviceBasicInfo> getDeviceBasicInfoByDeviceIds(Set<UUID> deviceIds) {
        return deviceCacheRepository.getDeviceBasicInfoByDeviceIds(deviceIds);
    }

    @Override
    public void deviceStatusChange(String deviceSerialNo, DeviceStatusEnum status) {
        deviceCacheRepository.deviceStatusChange(deviceSerialNo, status);
    }

    @Override
    public List<DeviceAttributeKey> getDeviceAttributeKeys(UUID deviceId, Set<String> attributeKeys) {
        return deviceFeignApi.deviceAttributeKeys(deviceId, attributeKeys);
    }

    @Override
    public void deviceControlPlanCallback(UUID requestId, String deviceSerialNo) {
        deviceFeignApi.deviceControlPlanCallback(requestId, deviceSerialNo);
    }

    @Override
    public List<DeviceAttributeKey> getDeviceAttributeKeys(UUID deviceId, Integer sn, Set<UUID> attributeIds) {
        return deviceFeignApi.deviceAttributeKeysByIds(deviceId, sn, attributeIds);
    }

    @Override
    public List<DeviceBasicInfo> getAllDeviceBasicInfo(Set<UUID> productIds) {
        return deviceCacheRepository.getAllDeviceBasicInfo(productIds);
    }
}
